package com.tsingteng.cosfun.utils;

import android.content.Context;
import android.util.TypedValue;

import com.tsingteng.cosfun.app.AppContext;


/**
 * 描述:
 * 创建人: 肖利
 * 时间: 2018/7/30
 * qq: (872179966)
 */

public class DesityUtils {



    private DesityUtils() {
          /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * dp转px
     *
     * @param dpVal
     * @return
     */
    public static int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, AppContext.getContext().getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     *
     * @param spVal
     * @return
     */
    public static int sp2px(float spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, AppContext.getContext().getResources().getDisplayMetrics());
    }

    /**
     * px转dp
     *
     * @param pxVal
     * @return
     */
    public static float px2dp(float pxVal) {
        final float scale = AppContext.getContext().getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    /**
     * px转sp
     *
     * @param pxVal
     * @param pxVal
     * @return
     */
    public static float px2sp(float pxVal) {
        return (pxVal / AppContext.getContext().getResources().getDisplayMetrics().scaledDensity);
    }

    public static int Dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }


    public static int getScale() {
        return (int) AppContext.getContext().getResources().getDisplayMetrics().density;
    }


}
